New xm uptime command.
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Sat, 12 Aug 2006 11:25:42 +0000 (12:25 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Sat, 12 Aug 2006 11:25:42 +0000 (12:25 +0100)
From Alex Brett <alex.brett@xensource.com>

Signed-off-by: Ewan Mellor <ewan@xensource.com>
docs/src/user.tex
tools/python/xen/xm/main.py

index eeec0721c07de52910264cd365e6de68a0e9a28e..8a0f7e3acbced01c3c05ef19d89e2717a1446df3 100644 (file)
@@ -1090,6 +1090,9 @@ The \path{xm list} command also supports a long output format when the
 \path{-l} switch is used.  This outputs the full details of the
 running domains in \xend's SXP configuration format.
 
+If you want to know how long your domains have been running for, then 
+you can use the \verb_# xm uptime_ command.
+
 
 You can get access to the console of a particular domain using 
 the \verb_# xm console_ command  (e.g.\ \verb_# xm console myVM_). 
index 799833a1ca4aa7947baf5d92d17c63a1a5f99c40..62b7a76f144fc87afcc207e3ebf88d9ff14d638d 100644 (file)
@@ -31,6 +31,7 @@ import warnings
 warnings.filterwarnings('ignore', category=FutureWarning)
 import xmlrpclib
 import traceback
+import datetime
 
 import xen.xend.XendProtocol
 
@@ -70,6 +71,7 @@ save_help =    "save <DomId> <File>              Save domain state (and config)
 shutdown_help ="shutdown <DomId> [-w][-a][-R|-H] Shutdown a domain"
 top_help =     "top                              Monitor system and domains in real-time"
 unpause_help = "unpause <DomId>                  Unpause a paused domain"
+uptime_help  = "uptime [-s|--short] [DomId, ...] List uptime for domains"
 
 help_spacer = """
    """
@@ -149,6 +151,7 @@ short_command_list = [
     "shutdown",
     "top",
     "unpause",
+    "uptime",
     "vcpu-set",
     ]
 
@@ -172,6 +175,7 @@ domain_commands = [
     "sysrq",
     "top",
     "unpause",
+    "uptime",
     "vcpu-list",
     "vcpu-pin",
     "vcpu-set",
@@ -412,6 +416,7 @@ def parse_doms_info(info):
         'vcpus'    : get_info('online_vcpus', int,   0),
         'state'    : get_info('state',        str,   '??'),
         'cpu_time' : get_info('cpu_time',     float, 0),
+        'up_time'  : get_info('up_time',      float, -1),
         'seclabel' : security.get_security_printlabel(info),
         }
 
@@ -818,6 +823,59 @@ def xm_console(args):
     domid = int(sxp.child_value(info, 'domid', '-1'))
     console.execConsole(domid)
 
+def xm_uptime(args):
+    short_mode = 0
+
+    try:
+        (options, params) = getopt.gnu_getopt(args, 's', ['short'])
+    except getopt.GetoptError, opterr:
+        err(opterr)
+        sys.exit(1)
+
+    for (k, v) in options:
+        if k in ['-s', '--short']:
+            short_mode = 1
+
+    doms = getDomains(params)
+
+    if short_mode == 0:
+        print 'Name                              ID Uptime'
+
+    for dom in doms:
+        d = parse_doms_info(dom)
+        if d['dom'] > 0:
+            uptime = int(round(d['up_time']))
+        else:
+            f=open('/proc/uptime', 'r')
+            upfile = f.read()
+            uptime = int(round(float(upfile.split(' ')[0])))
+            f.close()
+
+        days = int(uptime / 86400)
+        uptime -= (days * 86400)
+        hours = int(uptime / 3600)
+        uptime -= (hours * 3600)
+        minutes = int(uptime / 60)
+        uptime -= (minutes * 60)
+        seconds = uptime
+            
+        upstring = ""
+        if days > 0:
+            upstring += str(days) + " day"
+            if days > 1:
+                upstring += "s"
+            upstring += ", "
+        upstring += '%(hours)2d:%(minutes)02d' % vars()
+
+        if short_mode:
+            now = datetime.datetime.now()
+            upstring = now.strftime(" %H:%M:%S") + " up " + upstring
+            upstring += ", " + d['name'] + " (" + str(d['dom']) + ")"
+        else:
+            upstring += ':%(seconds)02d' % vars()
+            upstring = ("%(name)-32s %(dom)3d " % d) + upstring
+
+        print upstring
 
 def xm_top(args):
     arg_check(args, "top", 0)
@@ -1117,6 +1175,7 @@ commands = {
     "save": xm_save,
     "reboot": xm_reboot,
     "shutdown": xm_shutdown,
+    "uptime": xm_uptime,
     "list": xm_list,
     # memory commands
     "mem-max": xm_mem_max,